/******************************************************************************
 * * setting_volte_switch.c - implementation of volte switch setting functions
 *
 * *(C) Copyright 2020 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "setting_global.h"

static lv_style_t *Style_Sw_Bg;
static lv_style_t *Style_Sw_Indic;
static lv_style_t *Style_Sw_Knob;

/**
 *
 * Display_Setting_Volte_Switch
 * param (in) obj: lv_obj_t*
 *       (in) key_sta: UI_KEY_STATUS
 *       (in) key_val: UI_KEY_VALUE
 * return UINT8: 0--call system handling outside
 *               1--handled inside
 */
UINT8 Display_Setting_Volte_Switch_Key_Cb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UI_Normal_Container_List_Desc_t *NormalContListDesc;
    UI_Container_Base_Desc_t        *ContList;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    NormalContListDesc = (UI_Normal_Container_List_Desc_t *)Nav_Get_Param(ACT_ID_SETTING_VOLTE_SWITCH);
    ContList           = NormalContListDesc->ContList.ContainerList;

    if (KEY_DOWN == key_val)
    {
        NormalContListDesc->ContList.Index++;
        NormalContListDesc->ContList.Index %= NormalContListDesc->ContList.ListContainerCnt;
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        NormalContListDesc->ContList.Index += NormalContListDesc->ContList.ListContainerCnt;
        NormalContListDesc->ContList.Index--;
        NormalContListDesc->ContList.Index %= NormalContListDesc->ContList.ListContainerCnt;
        return(0);
    }
    else if (KEY_LEFT == key_val)
    {
        if (1 == ContList[NormalContListDesc->ContList.Index].u.Sw.State)
        {
            printf("%s: switch off\n", __FUNCTION__);
            ContList[NormalContListDesc->ContList.Index].u.Sw.State = 0;
            lv_switch_off(ContList[NormalContListDesc->ContList.Index].u.Sw.sw, LV_ANIM_OFF);
        }
        return(1);
    }
    else if (KEY_RIGHT == key_val)
    {
        if (0 == ContList[NormalContListDesc->ContList.Index].u.Sw.State)
        {
            printf("%s: switch on\n", __FUNCTION__);
            ContList[NormalContListDesc->ContList.Index].u.Sw.State = 1;
            lv_switch_on(ContList[NormalContListDesc->ContList.Index].u.Sw.sw, LV_ANIM_OFF);
        }
        return(1);
    }
    else if (KEY_OK == key_val) // set volte state to modem
    {
        if (ContList[0].u.Sw.State != MMI_Modem_Get_Volte_State_Req(MMI_MODEM_SIM_1))
        {
            printf("%s: set volte state for sim1 %d\n", __FUNCTION__, ContList[0].u.Sw.State);
            MMI_ModemAdp_Set_Volte_State_Req(MMI_MODEM_SIM_1, ContList[0].u.Sw.State);
        }
        if (ContList[1].u.Sw.State != MMI_Modem_Get_Volte_State_Req(MMI_MODEM_SIM_2))
        {
            printf("%s: set volte state for sim2 %d\n", __FUNCTION__, ContList[1].u.Sw.State);
            MMI_ModemAdp_Set_Volte_State_Req(MMI_MODEM_SIM_2, ContList[1].u.Sw.State);
        }
        Display_Setting_Done(ACT_ID_SETTING_VOLTE_SWITCH, PHONE_TEXT_ID_DONE, ACT_ID_SETTING_VOLTE_SWITCH);
        return(1);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        Nav_Back(ACT_ID_ANY);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
}

/**
 * OnDestroy function for volte switch
 * param (in) Para: VOID *
 * return  VOID
 */
VOID Display_Setting_Volte_Switch_OnDestroy(VOID * Para)
{
    printf("%s\n", __FUNCTION__);

    Hal_Mem_Free(Style_Sw_Bg);
    Hal_Mem_Free(Style_Sw_Indic);
    Hal_Mem_Free(Style_Sw_Knob);
}

/**
 * Common function for OnCreate and OnRestore function for volte switch
 * param (in) Para: VOID *
 * return  VOID
 */
VOID Display_Setting_Volte_Switch_Com_Setting(VOID * Para)
{
    UI_Normal_Container_List_Desc_t * Desc = (UI_Normal_Container_List_Desc_t *)Para;
    UINT8 i;

    for (i = 0;i < Desc->ContList.ListContainerCnt;i++)
    {
        lv_obj_add_style(Desc->ContList.ContainerList[i].u.Sw.sw,
                         LV_SWITCH_PART_BG,
                         Style_Sw_Bg);
        lv_obj_add_style(Desc->ContList.ContainerList[i].u.Sw.sw,
                         LV_SWITCH_PART_INDIC,
                         Style_Sw_Indic);
        lv_obj_add_style(Desc->ContList.ContainerList[i].u.Sw.sw,
                         LV_SWITCH_PART_KNOB,
                         Style_Sw_Knob);
    }

    // display operation instruction
    lv_obj_t * Par = lv_obj_get_parent(Desc->List);
    lv_obj_t * OperLabel = lv_label_create(Par, NULL);
    lv_label_set_long_mode(OperLabel, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(OperLabel, FP_HOR_RES * 4 / 5);
    lv_label_set_align(OperLabel, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(OperLabel, PHONE_TEXT_ID_VOLTE_SWITCH_NOTE);
    lv_obj_align(OperLabel, Par, LV_ALIGN_CENTER, 0, 20);
}

/**
 * OnRestore function for volte switch
 * param (in) Para: VOID *
 * return  VOID
 */
VOID Display_Setting_Volte_Switch_OnRestore(VOID * Para)
{
    printf("%s\n", __FUNCTION__);

    Display_Setting_Volte_Switch_Com_Setting(Para);
}

/**
 * OnCreate function for volte switch
 * param (in) Para: VOID *
 * return  VOID
 */
VOID Display_Setting_Volte_Switch_OnCreate(VOID * Para)
{
    printf("%s\n", __FUNCTION__);

    Style_Sw_Bg    = (lv_style_t *)Hal_Mem_Alloc(sizeof(lv_style_t));
    Style_Sw_Indic = (lv_style_t *)Hal_Mem_Alloc(sizeof(lv_style_t));
    Style_Sw_Knob  = (lv_style_t *)Hal_Mem_Alloc(sizeof(lv_style_t));
    lv_style_init(Style_Sw_Bg);
    lv_style_init(Style_Sw_Indic);
    lv_style_init(Style_Sw_Knob);
    lv_style_copy(Style_Sw_Bg, &lv_style_plain);
    lv_style_copy(Style_Sw_Indic, &lv_style_plain);
    lv_style_copy(Style_Sw_Knob, &lv_style_plain);

    lv_style_set_radius(Style_Sw_Bg, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_style_set_bg_color(Style_Sw_Bg, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_style_set_bg_grad_color(Style_Sw_Bg, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_style_set_pad_left(Style_Sw_Bg, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_right(Style_Sw_Bg, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_top(Style_Sw_Bg, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_bottom(Style_Sw_Bg, LV_STATE_DEFAULT, 0);
    lv_style_set_shadow_width(Style_Sw_Bg, LV_STATE_DEFAULT, 0);
    lv_style_set_border_opa(Style_Sw_Bg, LV_STATE_DEFAULT, LV_OPA_0);

    lv_style_set_radius(Style_Sw_Indic, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_style_set_bg_color(Style_Sw_Indic, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x0B,0x85,0xFF));
    lv_style_set_bg_grad_color(Style_Sw_Indic, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x0B,0x85,0xFF));
    lv_style_set_pad_left(Style_Sw_Indic, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_right(Style_Sw_Indic, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_top(Style_Sw_Indic, LV_STATE_DEFAULT, 0);
    lv_style_set_pad_bottom(Style_Sw_Indic, LV_STATE_DEFAULT, 0);
    lv_style_set_shadow_width(Style_Sw_Indic, LV_STATE_DEFAULT, 0);
    lv_style_set_border_opa(Style_Sw_Indic, LV_STATE_DEFAULT, LV_OPA_0);

    lv_style_set_radius(Style_Sw_Knob, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_style_set_bg_color(Style_Sw_Knob, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_bg_grad_color(Style_Sw_Knob, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_shadow_width(Style_Sw_Knob, LV_STATE_DEFAULT, 0);
    lv_style_set_border_opa(Style_Sw_Knob, LV_STATE_DEFAULT, LV_OPA_0);
    lv_style_set_pad_left(Style_Sw_Knob, LV_STATE_DEFAULT, -3);
    lv_style_set_pad_right(Style_Sw_Knob, LV_STATE_DEFAULT, -3);
    lv_style_set_pad_top(Style_Sw_Knob, LV_STATE_DEFAULT, -3);
    lv_style_set_pad_bottom(Style_Sw_Knob, LV_STATE_DEFAULT, -3);

    Display_Setting_Volte_Switch_Com_Setting(Para);
}

/**
 * display Setting_Volte_Switch
 * param (in) void:
 * return void:
 */
VOID Display_Setting_Volte_Switch(VOID)
{
    UI_Normal_Container_List_Desc_t *NormalContListDesc;
    UI_Container_Base_Desc_t        *ContList;
    Nav_Func_List_t                 FuncList;
    UINT32                          Size;

    NormalContListDesc =
        (UI_Normal_Container_List_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_Container_List_Desc_t));
    Hal_Mem_Set(NormalContListDesc, 0, sizeof(UI_Normal_Container_List_Desc_t));

    NormalContListDesc->TitleBar.TxtId      = PHONE_TEXT_ID_VOLTE_SWITCH;
    NormalContListDesc->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;
    NormalContListDesc->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_LEFT;

    NormalContListDesc->ContList.ListContainerCnt = 2;
    Size = sizeof(UI_Container_Base_Desc_t) * NormalContListDesc->ContList.ListContainerCnt;
    NormalContListDesc->ContList.ContainerList = (UI_Container_Base_Desc_t *)Hal_Mem_Alloc(Size);
    Hal_Mem_Set(NormalContListDesc->ContList.ContainerList, 0, Size);
    ContList = NormalContListDesc->ContList.ContainerList;

    ContList[0].Valid             = TRUE;
    ContList[0].Label.TxtId       = PHONE_TEXT_ID_SIM1;
    ContList[0].Label.TxtAlign    = LV_LABEL_ALIGN_LEFT;
    ContList[0].Label.LabelAlign  = LV_ALIGN_IN_LEFT_MID;
    ContList[0].Choise            = UI_BASE_OBJ_SW_ONE_ROW;
    ContList[0].u.Sw.State        = MMI_Modem_Get_Volte_State_Req(MMI_MODEM_SIM_1);

    ContList[1].Valid             = TRUE;
    ContList[1].Label.TxtId       = PHONE_TEXT_ID_SIM2;
    ContList[1].Label.TxtAlign    = LV_LABEL_ALIGN_LEFT;
    ContList[1].Label.LabelAlign  = LV_ALIGN_IN_LEFT_MID;
    ContList[1].Choise            = UI_BASE_OBJ_SW_ONE_ROW;
    ContList[1].u.Sw.State        = MMI_Modem_Get_Volte_State_Req(MMI_MODEM_SIM_2);
    printf("%s: volte state, sim1 %d, sim2 %d\n",
           __FUNCTION__, ContList[0].u.Sw.State, ContList[1].u.Sw.State);

    NormalContListDesc->ButtonBar.ButtonM.Valid = TRUE;
    NormalContListDesc->ButtonBar.ButtonM.TxtId = PHONE_TEXT_ID_OK;
    NormalContListDesc->ButtonBar.ButtonR.Valid = TRUE;
    NormalContListDesc->ButtonBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;

    NormalContListDesc->KeyFunc = Display_Setting_Volte_Switch_Key_Cb;

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow         = UI_Normal_Container_List_Create;
    FuncList.OnCreate       = Display_Setting_Volte_Switch_OnCreate;
    FuncList.OnRestoreState = Display_Setting_Volte_Switch_OnRestore;
    FuncList.OnDestroy      = Display_Setting_Volte_Switch_OnDestroy;

    Nav_Forward(ACT_ID_ANY, ACT_ID_SETTING_VOLTE_SWITCH, &FuncList, NormalContListDesc);
} /* Display_Setting_Volte_Switch */

VOID Display_Setting_Volte_Switch_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED == Event)
    {
        Display_Setting_Volte_Switch();
    }
}
